define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cost/cost/index' + location.search,
                   /* add_url: 'cost/cost/add',
                    edit_url: 'cost/cost/edit',*/
                    del_url: 'cost/cost/del',
                    multi_url: 'cost/cost/multi',
                    import_url: 'cost/cost/import',
                    table: 'cost_cost',
                }
            });

            var table = $("#table");
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                $("#money").text(data.money);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        /*{field: 'cost_category_id', title: __('Cost_category_id')},*/
                        {field: 'warranty_id', title: __('Warranty_id')},
                        {field: 'warranty.workID', title: __('Warranty.workID')},
                        {field: 'billing_data', title: __('Billing_data'), searchList: {"0":__('Billing_data 0'),"1":__('Billing_data 1')}, formatter: Table.api.formatter.normal},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        /*{field: 'user_id', title: __('User_id')},*/
                        /*{field: 'type_data', title: __('Type_data'), searchList: {"0":__('Type_data 0'),"1":__('Type_data 1'),"2":__('Type_data 2'),"3":__('Type_data 3'),"4":__('Type_data 4')}, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'offer_money', title: __('Offer_money'), operate:'BETWEEN'},
                        {field: 'number', title: __('Number')},*/
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'a.name', title: __('Warranty.product'), operate: 'LIKE'},
                        {field: 'a.marking', title: __('Warranty.device'), operate: 'LIKE'},
                        {field: 'category.name', title: __('Category.name'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user.avatar', title: __('User.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'see',
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('详情'),   icon: '',
                                    url: 'cost/cost/see'
                                },
                                {
                                    name: 'ok',
                                    title:'通过',
                                    text: '通过',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定通过审核？',
                                    url: function (row, column) {
                                        var url = 'cost/cost/operation?ids=' + row.id+'&status=1';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                                {
                                    name: 'operation',
                                    classname: 'btn btn-xs btn-info btn-refund',
                                    text: "拒绝",
                                    icon: "",
                                    visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status!=0 && row.status!= 2) {
                                            return false
                                        }
                                        return true;

                                    }
                                },
                            ],
                            events: {
                                'click .btn-refund': function (e, value, row) {
                                    Layer.prompt({
                                        title: "请输入原因",
                                        formType: 2,
                                        area: ['200px', '100px'] //自定义文本域宽高
                                    }, function (value) {
                                        Fast.api.ajax({
                                            url: "cost/cost/operation?ids=" + row.id+'&status=2',
                                            data: {remarks: value},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                        });
                                    });
                                    return false;
                                },

                                'click .btn-delone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var that = this;
                                    var top = $(that).offset().top - $(window).scrollTop();
                                    var left = $(that).offset().left - $(window).scrollLeft() - 260;
                                    if (top + 154 > $(window).height()) {
                                        top = top - 154;
                                    }
                                    if ($(window).width() < 480) {
                                        top = left = undefined;
                                    }
                                    Layer.confirm(
                                        __('Are you sure you want to delete this item?'),
                                        {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true, btn: [__('OK'), __('Cancel')]},
                                        function (index) {
                                            var table = $(that).closest('table');
                                            var options = table.bootstrapTable('getOptions');
                                            Table.api.multi("del", row[options.pk], table, that);
                                            Layer.close(index);
                                        }
                                    );
                                }
                            },formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            table.bootstrapTable('hideColumn','warranty_id');
            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", ".btn-export", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("cost/cost/export") + "' method='post' target='_blank'>" +
                    "<input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'>" +
                    "<input type='hidden' name='search'><input type='hidden' name='columns'>" +
                    "</form>", {
                    title: '导出数据',
                    btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    }
                    , yes: function (index, layero) {
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn3: function (index, layero) {
                        submitForm("all", layero);
                        return false;
                    }
                })
            });
            var submitForm = function (ids, layero) {
                var options = table.bootstrapTable('getOptions');
                console.log(options);
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                    if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                        columns.push(j.field);
                    }
                });
                var search = options.queryParams({});
                console.log(search);
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                $("input[name=columns]", layero).val(columns.join(','));
                $("form", layero).submit();
            };
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        operate: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
$(".nav-tabs li").click(function () {
    var val=$(this).children("a").attr("data-value");
    if(val==1){
        $(".btn-export").removeClass("hide");
    }else{
        $(".btn-export").addClass("hide");
    }
})